﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Utility
{
    public static class TimeHelp
    {
        /// <summary>
        /// 获取长时间的年月日
        /// </summary>
        /// <param name="time">时间(1990-12-21 10:45:55)</param>
        /// <returns>yyyy-MM-dd</returns>
        public static string GetDateYMD(string time)
        {
            int s = time.IndexOf(' ');
            if (s != -1)
            {
                time = time.Substring(0, s);
            }            
            return time;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string GetDateYMD(object time)
        {
            DateTime dt = Convert.ToDateTime(time);
            return (dt.Year + "年" + dt.Month + "月" + dt.Day + "日");
        }

        public static string GetDateHMS(object time)
        {
            DateTime dt = Convert.ToDateTime(time);
            return (dt.Hour + "点" + dt.Minute + "分" + dt.Second + "秒");
        }
        

        /// <summary>
        /// 获取长时间的月-日 时:分
        /// </summary>
        /// <param name="time">时间(1990-12-21 10:45:55)</param>
        /// <returns>MM-dd hh:mm</returns>
        public static string GetDatetime(string time)
        {
            int s = time.IndexOf('-') + 1;
            int s2 = time.LastIndexOf(':');
            if (s < s2)
            {
                time = time.Substring(s, s2 - s);
            }
            return time;
        }


        /// <summary>
        /// 获取长时间的月-日 时:分
        /// </summary>
        /// <param name="time">时间(1990-12-21 10:45:55)</param>
        /// <returns>MM-dd hh:mm</returns>
        public static int GetDateC(DateTime starttime, DateTime endtime)
        {
               
            TimeSpan dt = endtime - starttime;

            return dt.Minutes;
        }

        /// <summary>
        /// 读取时间戳
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static long ToLong(DateTime time)
        {
            return time.ToBinary();
        }
    }
}
